package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.collector.Collector;
import org.top10.collector.Filter;
import org.top10.entity.News;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class IfengTask extends Task {

	private static final int webSiteID = TOP10Global.WEB_SITE_ID_IFENG;// 对应网站ID
	private static final String ENCoding = "utf-8";// 对应网站ID

	public IfengTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new ClickHourParser());
		PARSER.add(new ClickDayParser());
		PARSER.add(new CommentHourParser());
		PARSER.add(new CommentDayParser());
	}

	public static void main(String args[]) throws Exception {
		String url = "http://news.ifeng.com/toprank/day/";
		//ParseSource source = Collector.createCache(url, ENCoding);
		new CommentHourParser().dealText(ENCoding);
	}
}

abstract class IfengParser extends TOPParserImpl {

	public IfengParser(String c, int t, String u) {
		super(c, t, u);
	}

	public abstract List parse(ParseSource doc) throws Exception;

	public abstract News build(Node node, int rank) throws Exception;

	// XUNlei所有解析器的公有方法放在这里..
	

	protected News buildNews(Node dl, int rank) {
		News v = new News();
		v.setRank(rank);
		v.setLinkUrl(getAttrValueByTag(dl, "href"));
		v.setName(dl.getTextContent());
		return v;
	}

	protected List ifengBuild(ParseSource source,final String sign) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		//Node node = getNodeByAttr("img", sign,"src", body);
			ArrayList<Node> dls = new ArrayList<Node>();
			
			Filter fitler=new Filter(){
				public boolean pass(Node node) {
					if(node==null)return false;
					String attr=getAttrValueByTag(node,"class");
					if(attr!=null&&attr.equals(sign))return true;
					return false;
				}
			};
			getAllTagsByParent(body, "a", dls,fitler);
			List<News> list = new ArrayList<News>();
			int rank = 1;
			for (Node d : dls) {
				News v = build(d, rank);
				if(v!=null){
					rank++;
					System.out.println(v.toString());
					list.add(v);
					if(rank==11)break;
				}
			}
			return list;
	}
}

class ClickHourParser extends IfengParser {
	private static final String SIGN = "sys_url";
	private static final String URL="http://news.ifeng.com/toprank/hour/";
	private static final String TITLE="一小时";
	public ClickHourParser() {
		super(TITLE, TOP10Global.TYPE_News, URL);
	}

	public News build(Node node, int rank) throws Exception {
		return buildNews(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return ifengBuild(doc,SIGN);
	}
	
}
class ClickDayParser extends IfengParser {
	private static final String SIGN = "sys_url";
	private static final String URL="http://news.ifeng.com/toprank/day/";
	private static final String TITLE="24小时";
	public ClickDayParser() {
		super(TITLE, TOP10Global.TYPE_News, URL);
	}

	public News build(Node node, int rank) throws Exception {
		return buildNews(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return ifengBuild(doc,SIGN);
	}
	
}
class CommentHourParser extends IfengParser {
	private static final String SIGN = "sys_url";
	private static final String URL="http://news.ifeng.com/comment/hour/";
	private static final String TITLE="一小时";
	public CommentHourParser() {
		super(TITLE, TOP10Global.TYPE_News, URL);
	}

	public News build(Node node, int rank) throws Exception {
		return buildNews(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return ifengBuild(doc,SIGN);
	}
	
}
class CommentDayParser extends IfengParser {
	private static final String SIGN = "sys_url";
	private static final String URL="http://news.ifeng.com/comment/day/";
	private static final String TITLE="24小时";
	public CommentDayParser() {
		super(TITLE, TOP10Global.TYPE_News, URL);
	}

	public News build(Node node, int rank) throws Exception {
		return buildNews(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return ifengBuild(doc,SIGN);
	}
	
}